using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace LenninSoftware.FormEditor
{
    public class FormControl: WebControl, INamingContainer
    {
        private FieldInfo _field;

        public FieldInfo Field
        {
            get { return _field; }
            set { _field = value; }
        }

        private IEditForm _form;

        public IEditForm Form
        {
            get { return _form; }
            set { _form = value; }
        }

        private SPControlMode _controlMode;

        public SPControlMode ControlMode
        {
            get { return _controlMode; }
            set { _controlMode = value; }
        }

        public FormControl(FieldInfo field, IEditForm form, IAdapterFactory adapterFactory, SPControlMode controlMode)
        {
            _field = field;
            _form = form;

            _controlMode = controlMode;
            _adapterFactory = adapterFactory;
        }

        private IAdapterFactory _adapterFactory;

        public IAdapterFactory AdapterFactory
        {
            get { return _adapterFactory; }
        }

        private Control _targetControl;

        public Control TargetControl
        {
            get { return _targetControl; }
            set { _targetControl = value; }
        }

        protected Control CreateTargetControl()
        {

            BaseControlAdapter adapter = AdapterFactory.Create(Field,
                (int)SPContext.Current.RegionalSettings.LocaleId,
                Form.Web);

            switch (ControlMode)
            {
                case SPControlMode.Display:
                    return adapter.CreateReadOnlyControl(Field);

                default:
                    return adapter.CreateControl(Field);            

            }
            
        }

        protected override void CreateChildControls()
        {
            _targetControl = CreateTargetControl();

            Controls.Add(_targetControl);           
        }

        public void LoadData()
        {
            
            EnsureChildControls();

            if (Form.Item != null)
            {

                BaseControlAdapter adapter = AdapterFactory.Create(Field,
                (int)SPContext.Current.RegionalSettings.LocaleId,
                Form.Web);

                object data = null;

                if (!(Field is IDonotUseFormData))                
                    data = Form.Item[Field.InternalName];

                switch (ControlMode)
                {
                    case SPControlMode.Display:
                        adapter.LoadReadOnlyData(TargetControl, Form.Item, data);
                        break;

                    default:
                        adapter.LoadData(TargetControl, Form.Item, data);
                        break;
                }                

            }
        }

        public bool IsEmptyAndRequired()
        {
            EnsureChildControls();

            if (!Field.Required)
                return false;

            BaseControlAdapter adapter = AdapterFactory.Create(Field,
                (int)SPContext.Current.RegionalSettings.LocaleId,
                Form.Web);

            return adapter.isEmpty(TargetControl);
        }

        public void Save(PropertyBag properties)
        {
            if (Form.ControlMode == SPControlMode.Display || Form.ControlMode == SPControlMode.Invalid)
                return;

            EnsureChildControls();

            BaseControlAdapter adapter = AdapterFactory.Create(Field,
            (int)SPContext.Current.RegionalSettings.LocaleId,
            Form.Web);

            if (Field is IDonotUseFormData)
                adapter.SaveData(TargetControl, Form.Item, null);
            else
            {
                object currentData = null;

                if (Form.Item != null)
                    currentData = Form.Item[Field.InternalName];

                object data = adapter.SaveData(TargetControl, Form.Item, currentData);

                properties[Field.InternalName] = data;
            }
        }

        public void Save()
        {
            if (Form.ControlMode == SPControlMode.Display || Form.ControlMode == SPControlMode.Invalid)
                return;

            EnsureChildControls();

            if (Form.Item != null)
            {
                BaseControlAdapter adapter = AdapterFactory.Create(Field,
                (int)SPContext.Current.RegionalSettings.LocaleId,
                Form.Web);

                if (Field is IDonotUseFormData)
                    adapter.SaveData(TargetControl, Form.Item, null);
                else
                {
                    object currentData = Form.Item[Field.InternalName];

                    object data = adapter.SaveData(TargetControl, Form.Item, currentData);
                    Form.Item[Field.InternalName] = data;
                }

            }

        }

        public void Reset()
        {
            EnsureChildControls();

            BaseControlAdapter adapter = AdapterFactory.Create(Field,
               (int)SPContext.Current.RegionalSettings.LocaleId,
               Form.Web);

            adapter.Reset(TargetControl);

        }

        public void LoadDefaults(PropertyBag defaults)
        {

            EnsureChildControls();

            BaseControlAdapter adapter = AdapterFactory.Create(Field,
            (int)SPContext.Current.RegionalSettings.LocaleId,
            Form.Web);

            object data = defaults[Field.InternalName];

            if (data == null)
                adapter.Reset(TargetControl);
            else
                adapter.LoadData(TargetControl, null, data);

        }


    }
}
